package com.jarvis.app.car;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationView;
import com.jarvis.app.car.fragment.HomeFragment;
import com.jarvis.app.car.fragment.MineFragment;
import com.jarvis.app.car.fragment.StaffFragment;

public class IndexActivity extends AppCompatActivity {

    private HomeFragment homeFragment;

    private MineFragment mineFragment;

    private StaffFragment staffFragment;

    private BottomNavigationView main_bottom_nv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_index);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        //初始化控件
        main_bottom_nv = findViewById(R.id.main_bottom_nv);

        //main_bottom_nv tab点击切换
        main_bottom_nv.setOnNavigationItemSelectedListener(
                menuItem -> {
                    menuItem.setChecked(true);
                    if (menuItem.getItemId() == R.id.home) {
                        selectedFragment(0);
                    } else if (menuItem.getItemId() == R.id.staff) {
                        selectedFragment(1);
                    } else if (menuItem.getItemId() == R.id.mine) {
                        selectedFragment(2);
                    }
                    return true;
                }


        );


        //默认首页选中
        selectedFragment(0);

    }


    public void selectedFragment(int position) {
        //获取fragmentManager管理器
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragment(transaction);
        if (position == 0) {
            if (homeFragment == null) {
                homeFragment = new HomeFragment();
                transaction.add(R.id.content, homeFragment);
            } else {
                homeFragment.loadData();
                transaction.show(homeFragment);
            }
        } else if (position == 1) {
            if (staffFragment == null) {
                staffFragment = new StaffFragment();
                transaction.add(R.id.content, staffFragment);
            } else {
                staffFragment.loadUserData();
                transaction.show(staffFragment);


            }
        } else if (position == 2) {
            if (mineFragment == null) {
                mineFragment = new MineFragment();
                transaction.add(R.id.content, mineFragment);
            } else {
                transaction.show(mineFragment);

            }
        }

        //最后设置提交     注意：这句话一定不能少！！！！！！
        transaction.commit();

    }


    private void hideFragment(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (mineFragment != null) {
            transaction.hide(mineFragment);
        }
        if (staffFragment != null) {
            transaction.hide(staffFragment);
        }


    }


}